package ntu.metaweb.storage;

import java.net.URL;
import java.util.HashMap;

public class Link extends Contribution {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1906298086415399169L;
	
	private URL fromURL;
	private URL toURL;
	private HashMap<String, String> userToNote;
	private String title;
	private double pageRating;
	
	public Link(URL fromURL, URL toURL) {
		this.fromURL = fromURL;
		this.toURL = toURL;
		this.userToNote = new HashMap<String, String>();
		this.title = "";
	}

	@Override
	public void addRating(String rater, int rating) {
		if (this.rating.containsKey(rater)) {
			this.rating.get(rater).setRating(rating);
		} else {
			this.rating.put(rater, new BinaryRating(rater, rating));
		}
	}

	public URL getFromURL() {
		return this.fromURL;
	}
	
	public URL getToURL () {
		return this.toURL;
	}
	
	@Override
	public int hashCode() {
		return this.fromURL.hashCode() + 2 * this.toURL.hashCode();
	}
	
	@Override
	public boolean equals(Object o) {
		if (!(o instanceof Link)) {
			return false;
		}
		Link link = (Link) o;
		return (this.fromURL.equals(link.fromURL)) && (this.toURL.equals(link.toURL));
	}

	public void addContributor(String user, String note) {
		this.addContributor(user);
		this.userToNote.put(user, note);
	}
	
	public String getUserNote(String user) {
		return this.userToNote.get(user);
	}
	
	public void setTitle(String title) {
		this.title = title;
	}
	
	public String getTitle() {
		return title;
	}

	public void setPageRating(double averageRating) {
		this.pageRating = averageRating;
	}
	
	public double getPageRating() {
		return this.pageRating;
	}

	public void deleteContributor(String user) {
		this.contributors.remove(user);
		this.userToNote.remove(user);
	}
}
